package com.tsing.wms.controller;


import com.tsing.wms.annotation.PassToken;
import com.tsing.wms.service.IUserService;
import com.tsing.wms.util.ResultUtil;
import com.tsing.wms.valid.AddGroup;
import com.tsing.wms.valid.UpdateGroup;
import com.tsing.wms.vo.PageResultVO;
import com.tsing.wms.vo.ResultData;
import com.tsing.wms.vo.req.*;
import com.tsing.wms.vo.resp.LoginRespVO;
import com.tsing.wms.vo.resp.UserRespVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author TheTsing
 * @since 2022-04-13
 */
@Api(tags = "用户模块")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @PassToken
    @PostMapping("/login")
    @ApiOperation(value = "用户登录")
    public ResultData<LoginRespVO> login(@Validated @RequestBody LoginReqVO reqVO) {
        return ResultUtil.success(userService.login(reqVO));
    }

    @PostMapping("/addUser")
    @ApiOperation(value = "新增用户")
    public ResultData addUser(@Validated({AddGroup.class}) @RequestBody UserReqVO reqVO, HttpServletRequest request) {
        userService.addUser(reqVO, request);
        return ResultUtil.success();
    }

    @PutMapping("/updateUser")
    @ApiOperation(value = "修改用户")
    public ResultData updateUser(@Validated({UpdateGroup.class}) @RequestBody UserReqVO reqVO, HttpServletRequest request) {
        userService.updateUser(reqVO, request);
        return ResultUtil.success();
    }

    @PutMapping("/updatePwd")
    @ApiOperation(value = "修改密码")
    public ResultData updatePwd(@Validated @RequestBody PwdUpdateReqVO reqVO, HttpServletRequest request) {
        userService.updatePwd(reqVO, request);
        return ResultUtil.success();
    }

    @PutMapping("/initPwd")
    @ApiOperation(value = "初始化密码")
    public ResultData initPwd(@Validated @RequestBody IdReqVO reqVO, HttpServletRequest request) {
        userService.initPwd(reqVO, request);
        return ResultUtil.success();
    }

    @GetMapping("/getUser")
    @ApiOperation(value = "查询用户详情")
    public ResultData<UserRespVO> getUser(@Validated IdReqVO reqVO) {
        return ResultUtil.success(userService.getUser(reqVO));
    }

    @GetMapping("/userPage")
    @ApiOperation(value = "用户分页")
    public ResultData<PageResultVO<UserRespVO>> userPage(@Validated UserPageReqVO reqVO) {
        return ResultUtil.success(userService.userPage(reqVO));
    }

    @DeleteMapping("/deleteUser")
    @ApiOperation(value = "删除用户")
    public ResultData deleteUser(@Validated @RequestBody IdsReqVO reqVO, HttpServletRequest request) {
        userService.deleteUser(reqVO, request);
        return ResultUtil.success();
    }

}
